﻿namespace Site2AppWP8
{
    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Tasks;
    
    public partial class SharePage : PhoneApplicationPage
    {
        private string link;
        private string message;

        public SharePage()
        {
            this.InitializeComponent();
            List<ShareItem> options = new List<ShareItem>();
            options.Add(new ShareItem("Email"));
            options.Add(new ShareItem("Messages"));
            options.Add(new ShareItem("Social networks"));
            this.ListSelector.ItemsSource = options;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.NavigationContext.QueryString.TryGetValue("LinkURL", out this.link);
            this.NavigationContext.QueryString.TryGetValue("Message", out this.message);
            base.OnNavigatedTo(e);
        }

        private void ListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (((ShareItem)((LongListSelector)sender).SelectedItem).Target)
            {
                case "Email":
                    EmailComposeTask ect = new EmailComposeTask();
                    ect.Body = HttpUtility.UrlDecode(this.message) + Environment.NewLine + Environment.NewLine + this.link;
                    ect.Show();
                    break;
                case "Messages":
                    SmsComposeTask sct = new SmsComposeTask();
                    sct.Body = HttpUtility.UrlDecode(this.message) + Environment.NewLine + Environment.NewLine + this.link;
                    sct.Show();
                    break;
                case "Social networks":
                    ShareLinkTask slt = new ShareLinkTask();
                    slt.LinkUri = new Uri(this.link);
                    slt.Show();
                    break;
                default:
                    break;
            }
        }
    }
}